//
//  AnimationHelper.swift
//  SmartUnderwear
//
//  Created by wsl on 16/10/18.
//  Copyright © 2016年 wsl. All rights reserved.
//

import UIKit
import pop

final class AnimationHelper: NSObject {
    
    private override init() {}
    
    static let helper = AnimationHelper()
    
    func animationY(view: UIView, fromValue: CGFloat, toValue: CGFloat) {
        let animation = POPSpringAnimation.init(propertyNamed: kPOPLayerPositionY)!
        animation.fromValue = fromValue
        animation.toValue = toValue
        animation.springBounciness = 10
        animation.springSpeed = 20
        animation.removedOnCompletion = false
        animation.isPaused = false
        view.layer.pop_add(animation, forKey: "kPOPLayerPositionY")
        let origin = view.frame.origin
        let size = view.bounds.size
        view.translatesAutoresizingMaskIntoConstraints = false
        view.frame = CGRect.init(x: origin.x, y: toValue, width: size.width, height: size.height)
    }
    
    
    func animation(shapeLayer: CAShapeLayer, fromValue: CGFloat, toValue: CGFloat) {
        let strokeAnimation = POPBasicAnimation.init(propertyNamed: kPOPShapeLayerStrokeEnd)!
        strokeAnimation.fromValue = fromValue
        strokeAnimation.toValue = toValue
        strokeAnimation.duration = 0.5
        shapeLayer.pop_add(strokeAnimation, forKey: "")
    }
    
    func animation(_ view: UIView, frame: CGRect) {
        let frameAnimation = POPSpringAnimation.init(propertyNamed: kPOPViewFrame)!
        frameAnimation.fromValue = NSValue.init(cgRect: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 0))
        frameAnimation.toValue = NSValue.init(cgRect: frame)
        frameAnimation.springBounciness = 5
        frameAnimation.springSpeed = 5
        view.pop_add(frameAnimation, forKey: "kPOPViewFrame")
        view.frame = frame
    }
    
    // 闪烁加放大动画
    func animationOpectiyAndScale(_ view:UIView) {
        let scaleVar =  1
        let opacitVar = 0.1
        let scaleAnimation = CAKeyframeAnimation.init(keyPath: "transform.scale")
        scaleAnimation.keyTimes = [NSNumber.init(value: 0.0), NSNumber.init(value: 0.45), NSNumber.init(value: 0.55), NSNumber.init(value: 1)]
        scaleAnimation.isRemovedOnCompletion = false
        scaleAnimation.values = [0.8, 1.2, 1.2, scaleVar]
        scaleAnimation.duration = 0.8
        
        let opacityAnimation = CAKeyframeAnimation.init(keyPath: "opacity")
        opacityAnimation.isRemovedOnCompletion = false
        opacityAnimation.keyTimes = [NSNumber.init(value: 0.0), NSNumber.init(value: 0.45), NSNumber.init(value: 0.55), NSNumber.init(value: 1)]
        opacityAnimation.values = [0.0, 1, 1, opacitVar]
        opacityAnimation.duration = 0.8
        
        let groupAnimation = CAAnimationGroup.init()
        groupAnimation.duration = 0.8
        groupAnimation.isRemovedOnCompletion = false
        groupAnimation.animations = [scaleAnimation,opacityAnimation]
        view.layer.add(groupAnimation, forKey: "FlickerAni")
    }
    
    // 闪烁动画
    func animationOpectiy(_ view:UIView) {
        let animation:CABasicAnimation = CABasicAnimation(keyPath: "opacity")
        animation.fromValue = NSNumber(value: 1.0)
        animation.toValue = NSNumber(value: 0.1)
        animation.autoreverses = true
        animation.duration = 0.8
        animation.repeatCount = Float(FP_INFINITE)
        animation.isRemovedOnCompletion = false
        animation.fillMode = kCAFillModeForwards
        animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
        view.layer.add(animation, forKey: "FlickerAni")
    }
    
    func animationFrame(view: UIView, toValue: CGRect,beginTime:CFTimeInterval,completed:(()->())?) {
        let springAnimation = POPSpringAnimation.init(propertyNamed: kPOPViewFrame)!
        springAnimation.toValue = NSValue.init(cgRect: toValue)
        springAnimation.springSpeed = 20
        springAnimation.springBounciness = 10
        springAnimation.beginTime = beginTime
        view.pop_add(springAnimation, forKey: nil)
        springAnimation.animationDidReachToValueBlock = {
            (_) in
            if (completed != nil) {
                completed!()
            }
        }
    }
    
}
